import { mLogin, logout, getInfo } from '@common/api/user';
import { /* staffSync, */ verifyStaff } from '@common/api/staff';
import { register } from '@common/api/organization';
import { getH5Token, setH5Token, removeH5Token } from '@common/utils/auth';
import { resetRouter } from '@h5/router';

const getDefaultState = () => ({
  token: getH5Token(),
  name: '',
  orgname: '',
  consoleUrl: '',
  showPopup: false,
  verified: true,
  gender: '',
  proposalStatus: '',
});

const state = getDefaultState();

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState());
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_ORGANIZATION: (state, orgname) => {
    state.orgname = orgname;
  },
  SET_VERIFIED: (state, verified) => {
    state.verified = verified;
  },
  SHOW_VERIFY_POPUP: (state, { consoleUrl, showPopup }) => {
    state.consoleUrl = consoleUrl;
    state.showPopup = showPopup;
  },
  HIDE_VERIFY_POPUP: state => {
    state.showPopup = false;
  },
  SET_GENDER: (state, gender) => {
    state.gender = gender;
  },
  SET_POPPOSAL_STATUS: (state, status) => {
    state.proposalStatus = status;
  },
};

const actions = {
  // user login
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      mLogin(userInfo)
        .then(response => {
          const { data } = response;
          commit('SET_TOKEN', data.token);
          setH5Token(data.token);
          // 同步用户信息
          resolve();
        })
        .catch(error => {
          reject(error);
        });
    });
  },
  register({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      register(userInfo)
        .then(response => {
          const { data } = response;
          commit('SET_TOKEN', data.token);
          commit('SET_NAME', data.userName);
          setH5Token(data.token);
          resolve(data);
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then(response => {
          const { data } = response;
          if (!data) {
            return reject('Verification failed, please Login again.');
          }

          const { name, organizationName, gender, proposalStatus } = data;
          commit('SET_NAME', name);
          commit('SET_ORGANIZATION', organizationName);
          commit('SET_GENDER', gender);
          commit('SET_POPPOSAL_STATUS', proposalStatus);
          // commit('SET_VERIFIED', verified);
          // if (verified) {
          //   staffSync().then(() => resolve(data));
          // } else {
          resolve(data);
          // }
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          removeH5Token(); // must remove  token  first
          resetRouter();
          commit('RESET_STATE');
          resolve();
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeH5Token(); // must remove  token  first
      commit('RESET_STATE');
      resolve();
    });
  },
  async showVerifyPopup({ commit }, userInfo) {
    const res = await verifyStaff({
      user: userInfo,
    });
    if (res.data.ConsoleUrl) {
      commit('SHOW_VERIFY_POPUP', {
        consoleUrl: res.data.ConsoleUrl,
        showPopup: true,
      });
    }
  },
  hideVerifyPopup({ commit }) {
    commit('HIDE_VERIFY_POPUP');
  },
  async switchOrg({ commit }, token) {
    commit('SET_TOKEN', token);
    setH5Token(token);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
